<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { getDoctorDetail } from '@/services/consult'
import { showToast } from 'vant'

const route = useRoute()
const router = useRouter()
const loading = ref(true)
const doctor = ref<{
  id: string
  doctorName: string
  personalResume: string | null
  message: string | null
  expertise: string | null
  socialPosition: string | null
  awards: string | null
  researchAchievements: string | null
  price: number
  avatar: string
  imgUrl: string
  departments: string | null
  avgScore: number | null
  consultCount: number | null
  finishedConsultCount: number | null
}>()

const loadDoctorDetail = async () => {
  try {
    const res = await getDoctorDetail(route.params.id as string)
    if (res.success) {
      doctor.value = res.data
    } else {
      showToast('获取医生信息失败')
    }
  } catch (e) {
    showToast('获取医生信息失败')
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  loadDoctorDetail()
})

const onShare = () => {
  // TODO: Implement share functionality
  showToast('分享功能开发中')
}

const onConsult = () => {
  router.push(`/consult/illness?doctorId=${doctor.value?.id}`)
}
</script>

<template>
  <div class="doctor-page">
    <cp-nav-bar title="医生详情" @click-right="onShare">
      <template #right><cp-icon name="share" /></template>
    </cp-nav-bar>

    <div v-if="!loading && doctor" class="doctor-detail">
      <!-- Doctor Profile -->
      <div class="doctor-card">
        <div class="info">
          <img :src="doctor.imgUrl" class="avatar" />
          <div class="name">
            <h1>{{ doctor.doctorName }}</h1>
            <span
              >{{ doctor.departments || '暂无科室' }} |
              {{ doctor.socialPosition || '暂无职称' }}</span
            >
          </div>
        </div>
        <div class="hospital">南京医科大学附属儿童医院</div>

        <!-- Stats -->
        <!-- Stats -->
        <div class="stats">
          <div class="item">
            <div class="value">
              <van-icon name="star" class="star-icon" />
              {{ doctor.avgScore || 0 }}
            </div>
            <div class="label">评分</div>
          </div>
          <div class="item">
            <div class="value">{{ doctor.consultCount || 0 }}</div>
            <div class="label">问诊量</div>
          </div>
          <div class="item">
            <div class="value">{{ doctor.finishedConsultCount || 0 }}</div>
            <div class="label">完成数</div>
          </div>
        </div>
      </div>

      <!-- Personal Introduction -->
      <div class="detail-card">
        <h3>个人简介</h3>
        <p>{{ doctor.personalResume || '暂无简介' }}</p>
      </div>

      <!-- Expertise -->
      <div class="detail-card">
        <h3>擅长</h3>
        <p>{{ doctor.expertise || '暂无擅长信息' }}</p>
      </div>

      <!-- Message -->
      <div class="detail-card" v-if="doctor.message">
        <h3>医生寄语</h3>
        <p>{{ doctor.message }}</p>
      </div>

      <!-- Research Achievements -->
      <div class="detail-card" v-if="doctor.researchAchievements">
        <h3>研究成果</h3>
        <p>{{ doctor.researchAchievements }}</p>
      </div>
    </div>

    <!-- Loading State -->
    <div v-else-if="loading" class="loading">
      <van-skeleton title avatar :row="3" />
    </div>

    <!-- Consultation Button -->
    <div class="consult-button">
      <div class="price">
        <span>图文问诊 {{ doctor?.price }}元</span>
      </div>
      <van-button type="primary" round @click="onConsult">问医生</van-button>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.doctor-page {
  padding-bottom: 50px;
  padding-top: 46px; /* 为导航栏留出空间，避免遮挡 */
  min-height: 100vh;
  background-color: var(--cp-bg);
}

.doctor-detail {
  .doctor-card {
    padding: 15px;
    background-color: #fff;
    border-radius: 8px;
    margin: 15px;
    box-shadow: 0 0 11px 0 rgba(229, 229, 229, 0.2);

    .info {
      display: flex;
      align-items: center;

      .avatar {
        width: 60px;
        height: 60px;
        border-radius: 50%;
        margin-right: 15px;
      }

      .name {
        h1 {
          font-size: 18px;
          margin: 0;
          padding: 0;
        }
        span {
          color: var(--cp-tip);
          font-size: 14px;
        }
      }
    }

    .hospital {
      margin-top: 8px;
      color: var(--cp-tip);
      font-size: 14px;
    }

    .stats {
      display: flex;
      margin-top: 15px;

      .item {
        flex: 1;
        text-align: center;

        .value {
          font-size: 16px;
          font-weight: bold;

          .star-icon {
            font-size: 14px;
            color: #fadb14;
            margin-right: 2px;
          }
        }

        .label {
          font-size: 12px;
          color: var(--cp-tip);
          margin-top: 5px;
        }
      }
    }
  }

  .detail-card {
    margin: 15px;
    padding: 15px;
    background-color: #fff;
    border-radius: 8px;

    h3 {
      font-size: 16px;
      margin: 0 0 10px;
      padding: 0;
    }

    p {
      color: var(--cp-text3);
      font-size: 14px;
      line-height: 1.5;
      margin: 0;
    }

    .review-header,
    .article-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 15px;

      p {
        color: var(--cp-tip);
        font-size: 12px;
      }
    }

    .review-list {
      .review-item {
        padding: 10px 0;
        border-bottom: 1px solid var(--cp-line);

        &:last-child {
          border-bottom: none;
        }

        .user {
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin-bottom: 5px;

          .time {
            color: var(--cp-tip);
            font-size: 12px;
          }
        }

        .score {
          margin-bottom: 5px;
        }

        .content {
          color: var(--cp-text2);
          font-size: 14px;
        }
      }
    }

    .article-list {
      .article-item {
        padding: 10px 0;
        border-bottom: 1px solid var(--cp-line);

        &:last-child {
          border-bottom: none;
        }

        h4 {
          font-size: 14px;
          margin: 0 0 5px;
          padding: 0;
        }

        p {
          color: var(--cp-tip);
          font-size: 12px;
        }
      }
    }

    .no-review,
    .no-article {
      text-align: center;
      color: var(--cp-tip);
      font-size: 14px;
      padding: 20px 0;
    }
  }
}

.consult-button {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #fff;
  display: flex;
  align-items: center;
  padding: 10px 15px;
  box-shadow: 0 -2px 10px 0 rgba(0, 0, 0, 0.1);

  .price {
    flex: 1;
    span {
      color: var(--cp-price);
      font-size: 16px;
      margin-right: 10px;
    }
  }

  .van-button {
    width: 120px;
    height: 40px;
    font-size: 16px;
  }
}

/* 修复导航栏遮挡问题 */
:deep(.van-nav-bar) {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  background-color: #fff;
}

.loading {
  padding: 15px;
}
</style>
